package il.ac.bgu.cs.heuristics.utils;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author Ronen Pinko
 * 
 */
public final class Validation{

	private final static Pattern VARIABLE_NAME_PATTERN = Pattern
	        .compile("^[\\?a-zA-Z_][\\w\\-]*");

	private Validation() {
	} // no instantiation

	public static <T> T ensureArgumentNeverNull(T o) {
		if (o == null) {
			throw new IllegalArgumentException("argument may not be null");
		}
		return o;
	}

	public static String ensureArgumentContainsText(String s) {
		if (!StringUtils.containsText(s)) {
			throw new IllegalArgumentException("argument must contain text");
		}
		return s;
	}

	public static <T> void ensureArgumentSizeEquals(Collection<T> c, int size) {
		if (c.size() != size) {
			throw new IllegalArgumentException("argument size should be ("
			        + size + ")");
		}
	}

	public static <T1, T2> void ensureAreSameSize(Collection<T1> c1,
	        Collection<T2> c2) {
		if (c1.size() != c2.size()) {
			throw new IllegalArgumentException(
			        "collections must be the same size col1.size=" + c1.size()
			                + " col2.size=" + c2.size());
		}
	}

	public static void ensureVariableNameConforms(String name) {
		ensureArgumentContainsText(name);
		final Matcher matcher = VARIABLE_NAME_PATTERN.matcher(name);
		if (!matcher.matches()) {
			throw new IllegalArgumentException("variable name - (\"" + name
			        + "\") doesn't conform to naming convention");
		}
	}

	public static void ensureArgumentIsPositiveInt(int i) {
		if (i < 0) {
			throw new IllegalArgumentException("argument must be positive");
		}
	}

	public static void ensureArgumentIsNaturalInt(int i) {
		ensureArgumentIsPositiveInt(i);
		if (i == 0) {
			throw new IllegalArgumentException("argument must be non-zero");
		}
	}

}
